import PageModel from "../../models/Page";
import Crypt from "../../utils/Crypt";
import config from "../../config/config";

const pageModel = new PageModel()

Page({
  data: {
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    baseUrl: config.app_base_url,
    // 商家列表
    merchantList: [],
    // 分类
    categoryList: [],
    // 产品
    productList: [],
    // 轮播
    swiperList: [],

    // 地区选择
    address_id: false

  },

  onTabItemTap(item) {

  },

  onShow: function () {
    // 查看有没有选择地区
    const address_id = wx.getStorageSync('Address_id')
    if (!address_id) {
      wx.showModal({
        title: '需要授权登录,才能继续,是否同意?',
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/center/index'
            })
          }
        }
      })
    } else {
      this.setData({ address_id: true })
    }

    // 可以会每次改变地区, 所以产品得改变
    this.setData({
      page: 0,
      pageSize: 10,
      list: [],
      busy: false,
    })
    this._getList()
  },

  onLoad: function (options) {



    // wx.getLocation({
    //   type: 'wgs84',
    //   success (res) {
    //     const latitude = res.latitude
    //     const longitude = res.longitude
    //     const speed = res.speed
    //     const accuracy = res.accuracy
    //     console.log(111, res)
    //   }
    // })

    // 统一请求
    pageModel.offlineUni().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      // console.log(data)
      const { swiper, merchant, category } = data
      this.setData({
        swiperList: swiper,
        merchantList: merchant
      })
      // 解决flex布局不足时, 第2+排时, start排
      // const n = 4
      // if (category.length > n || category.length < n) {
      //   const x = data.category.length % n
      //   if (x > 0) {
      //     for (let i = 0; i < (n - x); i++) {
      //       category.push(i)
      //     }
      //   }
      // }
      // this.setData({
      //   categoryList: category
      // })
      // console.log(this.data.categoryList)
      const n = 4
      if (merchant.length > n || merchant.length < n) {
        const x = data.merchant.length % n
        if (x > 0) {
          for (let i = 0; i < (n - x); i++) {
            merchant.push(i)
          }
        }
      }
      this.setData({
        merchantList: merchant
      })
    })


    // this._getList()
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      pageModel.offlineProduct({ ...this.data }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.list.length === 0) {
          this.setData({ busy: true })
        } else {
          this.setData({ busy: false })
          if (append) {
            this.setData({ productList: this.data.productList.concat(data.list) })
          } else {
            this.setData({ productList: data.list })
          }
        }

        console.log(this.data.productList)

      })
    }
  },

  // 轮播图跳转
  handleSwiper(event) {
    // todo 这里要分区是否为tabbar
    const item = event.currentTarget.dataset.item
    if (item && item.url && item.url.length > 1) {
      wx.navigateTo({
        url: item.url
      })
    }
  },

  // 商家跳转
  // handleMerchant(event) {
  //   const item = event.currentTarget.dataset.item
  //   // console.log(item.url)
  //   if (item && item.url && item.url.length > 1) {
  //
  //   }
  // },

  // 分类跳转
  bindCategory(event) {
    const item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: `/sub_product/pages/merchant_category/index?id=${item.id}`
    })
  },

  // 商家跳转
  handleMerchant(event) {
    const item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: `/sub_product/pages/merchant-product/index?id=${item.id}`
    })
  }


});
